define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index',
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    table: 'order',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE %...%'},
                        {field: 'user.username', title:'用户', operate: 'LIKE %...%'},
                        {field: 'business.name', title: '商家', operate: 'LIKE %...%'},
                        {field: 'order_status', title: __('Order_status'), visible:false, searchList: Config.orderStatusList},
                        {field: 'order_status_text', title: __('Order_status'), operate:false},
                        {field: 'shipping_status', title: __('Shipping_status'), visible:false, searchList: Config.shippingStatusList},
                        {field: 'shipping_status_text', title: __('Shipping_status'), operate:false},
                        {field: 'pay_status', title: __('Pay_status'), visible:false, searchList: Config.payStatusList},
                        {field: 'pay_status_text', title: __('Pay_status'), operate:false},
                        // {field: 'shipping_code', title: __('Shipping_code')},
                        // {field: 'shipping_id', title: __('Shipping_id')},
                        // {field: 'pay_code', title: __('Pay_code')},
                        {field: 'pay_type', title: __('Pay_name'), searchList:Config.payTypeList, visible: false},
                        {field: 'pay_type_text', title: __('Pay_name'), operate: false},
                        // {field: 'invoice_title', title: __('Invoice_title')},
                        // {field: 'goods_amount', title: __('Goods_amount'), operate:'BETWEEN'},
                        {field: 'freight', title: __('Freight'), operate:'BETWEEN'},
                        {field: 'coupon_price', title: __('Coupon_price'), operate:'BETWEEN'},
                        // {field: 'integral', title: __('Integral')},
                        // {field: 'integral_money', title: __('Integral_money'), operate:'BETWEEN'},
                        {field: 'order_amount', title: __('Order_amount'), operate:'BETWEEN'},
                        // {field: 'total_amount', title: __('Total_amount'), operate:'BETWEEN'},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'shipping_time', title: __('Shipping_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'confirm_time', title: __('Confirm_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'transaction_id', title: __('Transaction_id')},
                        {field: 'order_prom_type', title: __('Order_prom_type'), visible:false, searchList: Config.orderPromTypeList},
                        {field: 'order_prom_type_text', title: __('Order_prom_type'), operate:false},
                        // {field: 'order_prom_id', title: __('Order_prom_id')},
                        // {field: 'order_prom_amount', title: __('Order_prom_amount'), operate:'BETWEEN'},
                        // {field: 'discount', title: __('Discount'), operate:'BETWEEN'},
                        {field: 'message', title: __('Message'), operate: 'LIKE %...%'},
                        {field: 'admin_message', title: __('Admin_message'), operate: 'LIKE %...%'},
                        // {field: 'is_distribut', title: __('Is_distribut'), visible:false, searchList: Config.isDistributList},
                        // {field: 'is_distribut_text', title: __('Is_distribut'), operate:false},
                        // {field: 'deleted', title: __('Deleted'), visible:false, searchList: {"1":__('Deleted 1')}},
                        // {field: 'deleted_text', title: __('Deleted'), operate:false},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {
                        //     field: 'operate',
                        //     title: __('Operate'),
                        //     table: table,
                        //     buttons: [{
                        //         name: 'edit',
                        //         text: '',
                        //         title: '详情',
                        //         icon: 'fa fa-eye',
                        //         classname: 'btn btn-xs btn-success btn-addtabs',
                        //         url: 'order/edit'
                        //     }],
                        //     events: Table.api.events.operate,
                        //     formatter: Table.api.formatter.operate
                        // },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                // {name: 'cancel', text: '取消订单', title: '取消订单', icon: '', classname: 'btn btn-xs btn-danger btn-addtabs', url: 'order/edit',},
                                // {
                                //     name: 'pay', 
                                //     text: '线下支付完成', 
                                //     title: '线下支付完成', 
                                //     icon: '', 
                                //     classname: 'btn btn-xs btn-danger btn-ajax', 
                                //     confirm: '确认更改订单状态为线下支付完成吗？', 
                                //     url: 'order/offlinePay',
                                //     success: function (data, ret) {
                                //         table.bootstrapTable('refresh', {});
                                //     }
                                // },
                                {
                                    name: 'ship', 
                                    text: '发货', 
                                    title: '发货', 
                                    icon: '', 
                                    classname: 'btn btn-xs btn-warning btn-dialog', 
                                    url: 'order/ship',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                }
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                // if(row.order_status != 1){
                                //     $(table).data("operate-pay", null);
                                // }


                                if(row.order_status != 2){
                                    $(table).data("operate-ship", null);
                                }

                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        ship: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});